package com.novartis.nims.imagedc.util;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;

public class FormattedTextFieldVerifier extends InputVerifier {

	private JLabel inputInvalidLabel = null;
	
	public FormattedTextFieldVerifier() {
		super();
	}
	
	public FormattedTextFieldVerifier(JLabel label) {
		super();
		inputInvalidLabel = label;
	}

	@Override
	public boolean verify(JComponent component) {
		if (component instanceof JFormattedTextField) {
			JFormattedTextField field = (JFormattedTextField)component;
			return field.isEditValid();
		}
		
		return false;
	}
	
	@Override
	public boolean shouldYieldFocus(JComponent component) {
		boolean shouldYield = super.shouldYieldFocus(component);
		
		if (inputInvalidLabel != null) {
			inputInvalidLabel.setVisible(!shouldYield);
		}
		
		return shouldYield;
	}

}
